package com.example.demo.controller;

import com.example.demo.entity.Result;
import com.example.demo.entity.vo.BookTypeVo;
import com.example.demo.entity.vo.BookVo;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;

    /**
     * 获取所有书籍
     * @return
     */
    @GetMapping("/getAllBook")
    @ResponseBody
    public Result getAllBook(){
        List<BookVo> bookVos = bookService.getAllBook();
        if (bookVos != null){
            Result result = Result.ok("查询成功");
            result.setData(bookVos.size());
            return result;
        }

        return Result.error("查询失败");
    }

    /**
     * 获取所有书籍
     * @return
     */
    @GetMapping("/getAllBookByPage")
    @ResponseBody
    public Result getAllBookByPage(int page,int num){
        List<BookVo> bookVos = bookService.getAllBookByPage(page,num);
        if (bookVos != null){
            Result result = Result.ok("查询成功");
            result.setData(bookVos);
            return result;
        }

        return Result.error("查询失败");
    }


    /**
     * 获取所有类型
     * @return
     */
    @GetMapping("/getAllType")
    @ResponseBody
    public Result getAllType(){
        List<BookTypeVo> allType = bookService.getAllType();
        if (allType != null){
            Result result = Result.ok("查询成功");
            result.setData(allType);
            return result;
        }
        return Result.error("查询失败");
    }


    /**
     * 根据类型获取书籍id
     * @param id 书的类型id
     * @return
     */
    @GetMapping("/getBookByType")
    @ResponseBody
    public Result getBookByType(int id,int page,int num){
        List<BookVo> bookByType = bookService.getBookByType(id,page,num);
        if (id == 0){
            bookByType = bookService.getAllBookByPage(page,num);
        }
        if (bookByType != null){
            Result result = Result.ok("查询成功");
            result.setData(bookByType);
            return result;
        }
        return Result.error("查询失败");
    }


    /**
     * 根据书籍的id获取书籍的信息
     * @param id
     * @return
     */
    @GetMapping("/getBookMessage")
    @ResponseBody
    public Result getBookMessage(int id){
        BookVo bookMessage = bookService.getBookMessage(id);
        if (bookMessage != null){
            Result result = Result.ok("查询成功");
            result.setData(bookMessage);
            return result;
        }
        return Result.error("查询失败");
    }


    /**
     * 根据作者或者名称查询
     * @param name
     * @param author
     * @return
     */
    @GetMapping("/getBookByTypeOrByName")
    @ResponseBody
    public Result getBookByTypeOrByName(String name,String author){
        System.out.println(name);
        System.out.println(author);
        List<BookVo> bookByTypeOrByName = bookService.getBookByTypeOrByName(author, name);
        Result result = Result.ok("查询成功");
        result.setData(bookByTypeOrByName);
        return result;
    }
}
